package com.ad.common.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class StringUtils {
	public static boolean isEmpty(String str) {
		if (str == null || "".equals(str)) {
			return true;
		} else {
			return false;
		}
	}

	public static char ascii2Char(int ASCII) {
		return (char) ASCII;
	}

	public static int char2ASCII(char c) {
		return (int) c;
	}

	public static String ascii2String(int[] ASCIIs) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < ASCIIs.length; i++) {
			sb.append((char) ascii2Char(ASCIIs[i]));
		}
		return sb.toString();
	}

	public static String ascii2String(String ASCIIs) {
		String[] ASCIIss = ASCIIs.split(",");
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < ASCIIss.length; i++) {
			sb.append((char) ascii2Char(Integer.parseInt(ASCIIss[i])));
		}
		return sb.toString();
	}

	public static int[] string2ASCII(String s) {// 字符串转换为ASCII码
		if (s == null || "".equals(s)) {
			return null;
		}

		char[] chars = s.toCharArray();
		int[] asciiArray = new int[chars.length];

		for (int i = 0; i < chars.length; i++) {
			asciiArray[i] = char2ASCII(chars[i]);
		}
		return asciiArray;
	}

	public static String getIntArrayString(int[] intArray) {
		return getIntArrayString(intArray, ",");
	}

	public static String getIntArrayString(int[] intArray, String delimiter) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < intArray.length; i++) {
			sb.append(intArray[i]).append(delimiter);
		}
		return sb.toString();
	}

	public static String getASCII(int begin, int end) {
		StringBuffer sb = new StringBuffer();
		for (int i = begin; i < end; i++) {
			sb.append(i).append(":").append((char) i).append("/t");
			// sb.append((char) i).append("/t");
			if (i % 10 == 0) {
				sb.append("/n");
			}
		}
		return sb.toString();
	}

	public static String getCHASCII(int begin, int end) {
		return getASCII(19968, 40869);
	}

	public static void showASCII(int begin, int end) {
		for (int i = begin; i < end; i++) {
			// System.out.print(i + ":" + (char) i + "/t");
			System.out.print((char) i + "/t");
			if (i % 10 == 0) {
				System.out.println();
			}
		}
	}

	public static void showCHASCII() {
		showASCII(19968, 40869);
	}

	public static void showIntArray(int[] intArray) {
		showIntArray(intArray, ",");
	}

	public static void showIntArray(int[] intArray, String delimiter) {
		for (int i = 0; i < intArray.length; i++) {
			System.out.print(intArray[i] + delimiter);
		}
	}

	public static void createFile(String filePathAndName, String fileContent)
			throws IOException {

		String filePath = filePathAndName;
		filePath = filePath.toString();
		File myFilePath = new File(filePath);
		if (!myFilePath.exists()) {
			myFilePath.createNewFile();
		}
		FileWriter resultFile = new FileWriter(myFilePath);
		PrintWriter myFile = new PrintWriter(resultFile);
		String strContent = fileContent;
		myFile.println(strContent);
		myFile.close();
		resultFile.close();
	}
	/*
	 * 将类似\u4e91\u5357\uff0c\u7389\u6eaa格式的unicode解析成字符串
	 */
	public synchronized static String decodeUnicode(String theString) {
		char aChar;
		int len = theString.length();
		StringBuffer outBuffer = new StringBuffer(len);
		for (int x = 0; x < len;) {
			aChar = theString.charAt(x++);
			if (aChar == '\\') {
				aChar = theString.charAt(x++);
				if (aChar == 'u') {
					int value = 0;
					for (int i = 0; i < 4; i++) {
						aChar = theString.charAt(x++);
						switch (aChar) {
						case '0':
						case '1':
						case '2':
						case '3':
						case '4':
						case '5':
						case '6':
						case '7':
						case '8':
						case '9':
							value = (value << 4) + aChar - '0';
							break;
						case 'a':
						case 'b':
						case 'c':
						case 'd':
						case 'e':
						case 'f':
							value = (value << 4) + 10 + aChar - 'a';
							break;
						case 'A':
						case 'B':
						case 'C':
						case 'D':
						case 'E':
						case 'F':
							value = (value << 4) + 10 + aChar - 'A';
							break;
						default:
							throw new IllegalArgumentException(
									"Malformed      encoding.");
						}

					}
					outBuffer.append((char) value);
				} else {
					if (aChar == 't') {
						aChar = '\t';
					} else if (aChar == 'r') {
						aChar = '\r';
					} else if (aChar == 'n') {
						aChar = '\n';
					} else if (aChar == 'f') {
						aChar = '\f';
					}
					outBuffer.append(aChar);
				}
			} else {
				outBuffer.append(aChar);
			}

		}
		return outBuffer.toString();

	}

	public static void main(String[] args) {

		String s = "好好学习！天天向上！————笑的自然 2009年3月11日";
		showIntArray(string2ASCII(s), " ");
		System.out.println();
		System.out.println("==" + string2ASCII(s));

		System.out.println("\u7b80\u4ecb\uff1a");

	}

}
